package com.mrlv.ht.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrlv.ht.pojo.Dept;
import com.mrlv.ht.pojo.DeptExample;
import com.mrlv.ht.service.IDeptService;



@Controller
public class DeptController {
	
	@Resource
	private IDeptService deptService;
	
	@RequestMapping("/sysadmin/dept/list")
	public String list(Model model) {
		List<Dept> depts = deptService.findAllDept();
		model.addAttribute("depts", depts);
		return "/sysadmin/dept/jDeptList";
	}
	
	@RequestMapping("/sysadmin/dept/tocreate")
	public String toCreatr(Model model) {
		List<Dept> depts = deptService.selectByExample(new DeptExample());
		model.addAttribute("depts", depts);
		return "/sysadmin/dept/jDeptCreate";
	}
	
	@RequestMapping("/sysadmin/dept/insert")
	public String insert(Dept dept) {
		deptService.insertSelective(dept);
		return "redirect:/sysadmin/dept/list";
	}
	
	@RequestMapping("/sysadmin/dept/toupdate")
	public String toUpdate(Model model, String deptId) {
		if(deptId == null) {
			return "redirect:/sysadmin/dept/list";
		}
		String[] deptIds = deptId.split(",");
		if(deptIds.length > 1) {
			model.addAttribute("err", "不可同时修改多个用户");
			return "forward:/sysadmin/dept/list";
		}
		Dept dept = deptService.selectByPrimaryKey(deptId);
		List<Dept> depts = deptService.selectByExample(new DeptExample());
		model.addAttribute("deptInfo", dept);
		model.addAttribute("depts", depts);
		return "/sysadmin/dept/jDeptUpdate";
	}
	
	@RequestMapping("/sysadmin/dept/update")
	public String update(Dept dept) {
		deptService.updateByPrimaryKeySelective(dept);
		return "redirect:/sysadmin/dept/list";
	}
	
	@RequestMapping("/sysadmin/dept/deleteone")
	public String deleteOne(String deptId) {
		if(deptId != null) {
			deptService.deleteByPrimaryKey(deptId);
		}
		return "redirect:/sysadmin/dept/list";
	}
	
	@RequestMapping("/sysadmin/dept/delete")
	public String deleteAll(String[] deptId) {
		if(deptId != null) {
			deptService.deleteBatch(deptId);
		}
		return "redirect:/sysadmin/dept/list";
	}
	
	@RequestMapping("/sysadmin/dept/stop")
	public String stop(String[] deptId) {
		if(deptId == null) {
			return "redirect:/sysadmin/dept/list";
		}
		Dept dept = new Dept();
		if(deptId.length == 1) {
			dept.setDeptId(deptId[0]);
			dept.setState(new BigDecimal(0));
			deptService.updateByPrimaryKeySelective(dept);
		} else if(deptId.length > 1) {
			deptService.updateBatch0(deptId);
		}
		return "redirect:/sysadmin/dept/list";
	}
	
	@RequestMapping("/sysadmin/dept/start")
	public String start(String[] deptId) {
		if(deptId == null) {
			return "redirect:/sysadmin/dept/list";
		}
		Dept dept = new Dept();
		if(deptId.length == 1) {
			dept.setDeptId(deptId[0]);
			dept.setState(new BigDecimal(1));
			deptService.updateByPrimaryKeySelective(dept);
		} else if(deptId.length > 1) {
			deptService.updateBatch1(deptId);
		}
		return "redirect:/sysadmin/dept/list";
	}
}
